#!/usr/bin/python

# A test program for EMA calculate
# EMAtoday = alpha * Pricetoday + ( 1 - alpha ) * EMAyesterday;

price = [5.3, 5.6, 5.8, 5.4, 5.1, 5.3, 5.5, 5.9, 6.1, 6.3, 6.5, 6.2, 6.1, 5.7, 5.4, 5.5, 5.7, 5.8, 5.6, 5.3, 5.5]

N = 12

alpha = 2.0/(N + 1)

def ema(today):
    '''
        A test program for EMA calculate
    '''
    index_today = today - 1
    price_today = price[index_today]

    if (today == 1):
        print "EMA" + str(today) + " : " + str(alpha * price_today)
        return alpha * price_today

    index_yesterday = index_today - 1
    price_yesterday = -1

    if(index_yesterday < 0):
        price_yesterday = 0
    else:
        price_yesterday = price[index_yesterday]

    if (today > 0):
        ema_today = alpha * price_today + (1 - alpha) * ema(today - 1)

    print "EMA" + str(today) + " : " + str(ema_today)
    return ema_today

ema(len(price))
